/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ModificarResponsable.java
 *
 * Created on 06/11/2013, 21:17:46
 */
package Guis.Responsable;

import Clases.Responsable;
import Controlador.ControladorResponsable;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import static Guis.Principal.Principal.jDesktopPane1;
import javax.swing.filechooser.FileNameExtensionFilter;
import static Guis.Principal.Principal.jDesktopPane1;

/**
 *
 * @author a1
 */
public class ModificarResponsable extends javax.swing.JDialog {

    /** Creates new form ModificarResponsable */
    private Responsable res;
    private ControladorResponsable cr;
    private File archivoElegido;

    public ModificarResponsable(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        cr = new ControladorResponsable();
        //****************//
        this.MaximoCaracteres100(jTextField1);
        this.MaximoCaracteres45(jTextField2);
        this.MaximoCaracteres150(jTextField3);
        this.BloquearCopiar(jTextField1);
        this.BloquearCopiar(jTextField2);
        this.BloquearCopiar(jTextField3);
        //********************************************************************//
        this.setResizable(false);
        this.setTitle("Modificar Responsable");
        Dimension desktopSize = jDesktopPane1.getSize();
        Dimension jInternalFrameSize = this.getSize();
        this.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                (desktopSize.height - jInternalFrameSize.height) / 2);
        //********************************************************************//
         this.jButton2.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override
            public void keyPressed(java.awt.event.KeyEvent e) {
                if (KeyEvent.VK_ENTER == e.getKeyCode()) {
                    modificar();
                }
            }
        });
        this.jButton3.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override
            public void keyPressed(java.awt.event.KeyEvent e) {
                if (KeyEvent.VK_ENTER == e.getKeyCode()) {
                    dispose();
                }
            }
        });
        this.jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override
            public void keyPressed(java.awt.event.KeyEvent e) {
                if (KeyEvent.VK_ENTER == e.getKeyCode()) {
                    ventana();
                }
            }
        });
    }
    
     private void BloquearCopiar(JTextField a) {
        // BLOQUEA LA ACCION COPIAR 
        InputMap map2 = a.getInputMap(JTextField.WHEN_FOCUSED);
        map2.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK), "null");
    }

    private void MaximoCaracteres150(JTextField a) {
        a.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                JTextField a = (JTextField) e.getSource();
                if (a.getText().length() == 150) {
                    getToolkit().beep();
                    e.consume();
                }
            }
        });
    }

    private void MaximoCaracteres45(JTextField a) {
        a.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                JTextField a = (JTextField) e.getSource();
                if (a.getText().length() == 45) {
                    getToolkit().beep();
                    e.consume();
                }
            }
        });
    }

    private void MaximoCaracteres100(JTextField a) {
        a.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                JTextField a = (JTextField) e.getSource();
                if (a.getText().length() == 100) {
                    getToolkit().beep();
                    e.consume();
                }
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Responsable"));

        jLabel1.setText("Nombre");

        jLabel2.setText("Apodo");

        jLabel3.setText("Email");

        jButton1.setText("Seleccionar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel4.setText("Foto");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(10, 10, 10)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel3))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextField3)
                                .addComponent(jTextField2))))
                    .addComponent(jLabel4))
                .addContainerGap(43, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(70, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(68, 68, 68))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton2.setText("Guardar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Cancelar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3)
                        .addGap(90, 90, 90))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        ventana();
}//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        this.dispose();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        modificar();
    }//GEN-LAST:event_jButton2ActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables

    void setIdResponsable(int id) {
        res = cr.getResponsable(id);
        jTextField1.setText(res.getNombre());
        jTextField2.setText(res.getApodo());
        jTextField3.setText(res.getEmail());
        archivoElegido = new File(res.getPathFoto());
        try {
            BufferedImage read = ImageIO.read(archivoElegido);
            Image imagen = read.getScaledInstance(125, 125, Image.SCALE_DEFAULT);
            jLabel5.setIcon(new ImageIcon(imagen));
        } catch (IOException ex) {
            this.archivoElegido = null;
            JOptionPane.showMessageDialog(rootPane, "Se produjo un error al cargar la imagen");
        } catch (NullPointerException ex) {
            JOptionPane.showMessageDialog(rootPane, "Se produjo un error al cargar la imagen");
            this.archivoElegido = null;
        }
    }

    private void modificar() {
        if (validar()) {
            int resultado = JOptionPane.showConfirmDialog(rootPane, "¿Desea modificar el responsable?", "Confirmación", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);

            if (resultado == JOptionPane.YES_OPTION) {
                res.setNombre(jTextField1.getText());
                res.setApodo(jTextField2.getText());
                res.setEmail(jTextField3.getText());
                res.setPathFoto(archivoElegido.getPath());
                switch (cr.modificarResponsable(res)) {
                    case -1:
                        JOptionPane.showMessageDialog(rootPane, "No se pudo modificar el responsable");
                        break;
                    case 0:
                        JOptionPane.showMessageDialog(rootPane, "Responsable modificado");
                        dispose();
                        break;
                    case 1:
                        JOptionPane.showMessageDialog(rootPane, "Email no disponible ");
                        jTextField3.grabFocus();
                        break;
                }
            }


        }
    }

    private void ventana() {
        JFileChooser fc = new JFileChooser();
        FileNameExtensionFilter filtroImagen = new FileNameExtensionFilter("Imagenes (jpg, jpeg, png)", "jpg", "png", "jpeg");
        fc.setFileFilter(filtroImagen);
        fc.setAcceptAllFileFilterUsed(false);
        int respuesta = fc.showOpenDialog(this);
        if (respuesta == JFileChooser.APPROVE_OPTION) {
            try {
                archivoElegido = fc.getSelectedFile();
                BufferedImage read = ImageIO.read(archivoElegido);
                Image imagen = read.getScaledInstance(125, 125, Image.SCALE_DEFAULT);
                jLabel5.setIcon(new ImageIcon(imagen));
            } catch (IOException ex) {
                this.archivoElegido = null;
                JOptionPane.showMessageDialog(rootPane, "Se produjo un error al cargar la imagen");
            } catch (NullPointerException ex) {
                JOptionPane.showMessageDialog(rootPane, "Se produjo un error al cargar la imagen");
                this.archivoElegido = null;
            }
        }
    }

    private boolean validar() {
        boolean res = true;

        if (jTextField3.getText().isEmpty()) {
            jLabel3.setForeground(Color.red);
            jTextField3.grabFocus();
            res = false;
        } else {
            jLabel3.setForeground(Color.black);
        }

        if (jTextField2.getText().isEmpty()) {
            jLabel2.setForeground(Color.red);
            jTextField2.grabFocus();
            res = false;
        } else {
            jLabel2.setForeground(Color.black);
        }

        if (jTextField1.getText().isEmpty()) {
            jLabel1.setForeground(Color.red);
            jTextField1.grabFocus();
            res = false;
        } else {
            jLabel1.setForeground(Color.black);
        }
        if (archivoElegido == null) {
            jLabel4.setForeground(Color.red);
            res = false;
        } else {
            jLabel4.setForeground(Color.black);
        }
        return res;
    }
}
